/*
 *
 * 名称：奇门遁甲窗口
 * 功能：奇门遁甲九宫显示窗口
 * 作者：清河张
 * 时间：2025.05.11
 *
 */

#include "qmdjwin.h"
#include "ui_qmdjwin.h"
#include <QKeyEvent>
#include <QClipboard>
#include <QMessageBox>
#include <QDesktopServices>
#include "base/zhu.h"

using namespace QMDJ;

QMDJWIN::QMDJWIN(QWidget *parent, QMGONG9& gong9)
    : QMainWindow(parent)
    , ui(new Ui::QMDJWIN)
    , _cur(nullptr)
    , _displays{0}
    , _focused(false)
    , _gong9(gong9)
{
    int item;
    ui->setupUi(this);
    ui->czbDockWidget->hide();
    // ui->gongjieTextBrowser->hide();

    item = 0;
    _displays[item++] = ui->textBrowser0;
    _displays[item++] = ui->textBrowser1;
    _displays[item++] = ui->textBrowser2;
    _displays[item++] = ui->textBrowser3;
    _displays[item++] = ui->textBrowser4;
    _displays[item++] = ui->textBrowser5;
    _displays[item++] = ui->textBrowser6;
    _displays[item++] = ui->textBrowser7;
    _displays[item++] = ui->textBrowser8;

    gaoliang(_gong9.zhg());

    ui->infoTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->infoTableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->infoTableWidget->horizontalHeader()->setFont(QFont(tr("隶书"), 14));


    ui->sizhuTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->sizhuTableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->ysgTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->ysgTableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeMode::Fixed);
    ui->ysgTableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->ysgTableWidget->setColumnWidth(0, 40);
    ui->ysgTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->statusBar->showMessage( "快捷键F1查询" );

    initcbx();
}

void QMDJWIN::bazishow(const BAZI& bz)
{
    int row = 0, col = 0;

    col = 0;

    ui->sizhuTableWidget->item(row, col++)->setText(bz._n.name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._y.name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._r.name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._s.name());
    row++;

    col = 0;
    ui->sizhuTableWidget->item(row, col++)->setText(bz._n.wx5().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._y.wx5().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._r.wx5().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._s.wx5().name());
    row++;

    col = 0;
    ui->sizhuTableWidget->item(row, col++)->setText(bz._n.shouzhi().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._y.shouzhi().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._r.shouzhi().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._s.shouzhi().name());
    row++;

    col = 0;
    ui->sizhuTableWidget->item(row, col++)->setText(bz._n.dun().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._y.dun().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._r.dun().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._s.dun().name());
    row++;

    col = 0;
    ui->sizhuTableWidget->item(row, col++)->setText(bz._n.kw().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._y.kw().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._r.kw().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._s.kw().name());
    row++;

    col = 0;
    ui->sizhuTableWidget->item(row, col++)->setText(bz._n._z.maxing().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._y._z.maxing().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._r._z.maxing().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._s._z.maxing().name());
    row++;
}

QMDJWIN::~QMDJWIN()
{
    delete ui;
}

void QMDJWIN::paipanxianshi( int ju, int yy, const GAN10& qi, const GAN10& luo, const BAZI& bz)
{
    bazishow(bz);

    ui->czbDockWidget->show();
    ui->gongjieTextBrowser->show();

    ui->infoTableWidget->item(0,0)->setText(YY(yy).name());
    ui->infoTableWidget->item(0,1)->setText(SHUZI(ju).name());
    ui->infoTableWidget->item(0,2)->setText(_gong9.teshu(EQM_YS_ZHFU)->_xing.name());
    ui->infoTableWidget->item(0,3)->setText(_gong9.teshu(EQM_YS_ZHSH)->_men.name());
    ui->infoTableWidget->item(0,4)->setText(qi.name());
    ui->infoTableWidget->item(0,5)->setText(luo.name());

    gaoliang(_gong9.chazhao(&QMGONG9::GONG::_shen, SHEN8::ZHIFUUU));
    g9show();
    return ;
}

void QMDJWIN::ysshow(const FANGAN::YSMAP & ysmap)
{
    int row = 0;
    ui->ysgTableWidget->setRowCount(ysmap.size());

    for(auto itor = ysmap.begin(); itor != ysmap.end(); ++itor, ++row)
    {
        const FANGAN::YONGSHEN & ys = itor->second;
        ui->ysgTableWidget->setItem(row, 0, new QTableWidgetItem(QString::number(ys._paifu)));
        ui->ysgTableWidget->setItem(row, 1, new QTableWidgetItem(ys._ming));
        ui->ysgTableWidget->setItem(row, 2, new QTableWidgetItem(ys._bianzhi));
        ui->ysgTableWidget->setItem(row, 3, new QTableWidgetItem(ys._nengji));
        ui->ysgTableWidget->setItem(row, 4, new QTableWidgetItem(ys._lg ? QString::number( ys._lg->gid() ) : "0"));

        ui->ysgTableWidget->item(row, 0)->setTextAlignment(Qt::AlignCenter);
        ui->ysgTableWidget->item(row, 1)->setTextAlignment(Qt::AlignCenter);
        ui->ysgTableWidget->item(row, 2)->setTextAlignment(Qt::AlignCenter);
        ui->ysgTableWidget->item(row, 3)->setTextAlignment(Qt::AlignCenter);
        ui->ysgTableWidget->item(row, 4)->setTextAlignment(Qt::AlignCenter);

        if(ys._lg)
        {
            ys._lg->shengxiangyi();
            _displays[ys._lg->_xu]->setText( ys._lg->_xiangyi);
        }

    }

    ui->ysgTableWidget->sortItems(0);
}

void QMDJWIN::g9show()
{
    for(int i =0; i < 9; ++i)
    {
        _displays[i]->setText( _gong9.xu(i)->_xiangyi);
    }

    this->show();
}

//method=1转，method=2飞
void QMDJWIN::tiaozheng(int obj, int method, int dw)
{
    switch(obj)
    {
    case EQM_YS_XING9:
        if(method) _gong9.zhuandong(&QMGONG9::GONG::_xing,dw);
        else _gong9.feidong(&QMGONG9::GONG::_xing,dw);
        break;
    case EQM_YS_MMEN8:
        if(method) _gong9.zhuandong(&QMGONG9::GONG::_men,dw);
        // else gong9.feidong(&qmdjpan::_men,dw);
        break;
    case EQM_YS_SHEN8:
        if(method) _gong9.zhuandong(&QMGONG9::GONG::_shen,dw);
        // else gong9.feidong(&qmdjpan::_shen,dw);
        break;
    case EQM_YS_TPGAN:
        if(method) _gong9.zhuandong(&QMGONG9::GONG::_tp,dw);
        else _gong9.feidong(&QMGONG9::GONG::_tp,dw);
        break;
    case EQM_YS_DPGAN:
        if(method) _gong9.zhuandong(&QMGONG9::GONG::_dp,dw);
        else _gong9.feidong(&QMGONG9::GONG::_dp,dw);
        break;
    default:
        break;

    }

    _gong9.neiguan();
    ui->gongjieTextBrowser->setText(_cur->_jiexi);
    g9show();
}

void QMDJWIN::gaoliang(QMGONG9::GONG* cur)
{
#define DEFAULT_FONT_SIZE  8
#define CUR_FONT_SIZE  12
#define FOCUSED_FONT_SIZE   40
    if(cur != _cur)
    {
        if(_cur)
        {
            _displays[_cur->_xu]->setLineWidth(0);
            if(_focused)
                _displays[_cur->_xu]->setFontPointSize(DEFAULT_FONT_SIZE);
            else
                _displays[_cur->_xu]->setFontPointSize(DEFAULT_FONT_SIZE);

            _displays[_cur->_xu]->setText( _cur->_xiangyi);
        }

        if(cur)
        {
            _displays[cur->_xu]->setLineWidth(1);
            if(_focused)
                _displays[cur->_xu]->setFontPointSize(FOCUSED_FONT_SIZE);
            else
                _displays[cur->_xu]->setFontPointSize(CUR_FONT_SIZE);
            ui->biaozhuLineEdit->setText(cur->_beizhu);

            ui->gongjieTextBrowser->setText(cur->_jiexi);
            _displays[cur->_xu]->setText( cur->_xiangyi);
        }

        if(_focused)
        {
            if(_cur) _displays[_cur->_xu]->hide();
            if(cur) _displays[cur->_xu]->show();
        }

        _cur = cur;
    }

    if(!_cur) _cur = _gong9.zhg();
}

void QMDJWIN::on_tToolButton0_clicked()
{
    tiaozheng(ui->tiaoComboBox->currentData().toInt(), 1, -1);
}

void QMDJWIN::on_tToolButton1_clicked()
{
    tiaozheng(ui->tiaoComboBox->currentData().toInt(), 1, 1);
}

void QMDJWIN::on_fToolButton0_clicked()
{
    tiaozheng(ui->tiaoComboBox->currentData().toInt(), 0, -1);
}

void QMDJWIN::on_ftoolButton1_clicked()
{
    tiaozheng(ui->tiaoComboBox->currentData().toInt(), 0, 1);
}

void QMDJWIN::on_houttianToolButton_clicked()
{
    if(_cur) gaoliang(_cur->houtian());
}

void QMDJWIN::on_xiantianToolButton_clicked()
{
    if(_cur) gaoliang(_cur->xiantian());
}

void QMDJWIN::on_jianglaiToolButton_clicked()
{
    QMGONG9::GONG* cur;
    cur = _gong9.chazhao(&QMGONG9::GONG::_tp, _cur->_dp)->ji0();
    gaoliang(cur);
}


void QMDJWIN::on_guoquToolButton_clicked()
{
    QMGONG9::GONG* cur;
    cur = _gong9.chazhao(&QMGONG9::GONG::_dp, _cur->_tp)->ji1();
    gaoliang(cur);
}



void QMDJWIN::on_yinganToolButton_clicked()
{
    QMGONG9::GONG* cur;
    cur = _gong9.chazhao(&QMGONG9::GONG::_tp, _cur->_yin)->ji1();
    gaoliang(cur);
}


void QMDJWIN::on_anganToolButton_clicked()
{
    QMGONG9::GONG* cur;
    cur = _gong9.chazhao(&QMGONG9::GONG::_tp, _cur->_an)->ji1();
    gaoliang(cur);
}


void QMDJWIN::fullshow()
{
    if(_focused)//缩小
    {
        for(int i = 0; i < 9; ++i)
        {
            if(i == _cur->_xu)
            {
                _displays[i]->setFontPointSize(CUR_FONT_SIZE);
                _displays[_cur->_xu]->setText( _cur->_xiangyi);
            }
            else
            {
                _displays[i]->setFontPointSize(DEFAULT_FONT_SIZE);
                _displays[i]->setText( _gong9.xu(i)->_xiangyi);
                _displays[i]->show();
            }
        }
    }
    else//放大
    {
        for(int i = 0; i < 9; ++i)
        {
            if(i == _cur->_xu)
            {
                _displays[i]->setFontPointSize(FOCUSED_FONT_SIZE);
                _displays[_cur->_xu]->setText( _cur->_xiangyi);
            }
            else
            {
                _displays[i]->hide();
            }
        }
    }

    _focused = !_focused;
}


void QMDJWIN::on_cunToolButton_clicked()
{
    if(_cur)
    {
        _cur->_beizhu = ui->biaozhuLineEdit->text().toUtf8();
        _cur->shengxiangyi();
        _displays[_cur->_xu]->setText( _cur->_xiangyi);
    }
}


void QMDJWIN::keyPressEvent(QKeyEvent *event)
{
    const char* helpinfo =
        "1-9 宫数\nx 销刑\nk 销空\np 销迫\nm 销墓\nd 放大\n+ 顺飞\n- 逆飞\n"
        "n 年干\ny 月干\nr 日干\ns 时干\nz 值符\n\n\nctrl+n 年支\nctrl+y 月支\nctrl+r 日支\n"
        "ctrl+s 时支\nctrl+m 马星\nctrl+z 值使\nctrl+d 转AI";
    if(event->modifiers() & Qt::ControlModifier)
    {
        switch ( event->key() ) {
        case Qt::Key_N :
            gaoliang(_gong9.teshu( EQM_YS_NZHI));
            break;
        case Qt::Key_Y :
            gaoliang(_gong9.teshu( EQM_YS_YZHI));
            break;
        case Qt::Key_R :
            gaoliang(_gong9.teshu( EQM_YS_RZHI));
            break;
        case Qt::Key_S :
            gaoliang(_gong9.teshu( EQM_YS_SZHI));
            break;
        case Qt::Key_M:
            gaoliang(_gong9.mx());
            break;
        case Qt::Key_Z:
            gaoliang(_gong9.teshu(EQM_YS_ZHSH));
            break;
        case Qt::Key_D:
            QApplication::clipboard()->setText(_gong9.jumian());
            QMessageBox::information(this, "即将进入AI", "局面已复制，直接贴上，并说明要断的内容");
            QDesktopServices::openUrl(QUrl("https://chat.baidu.com/"));
            break;
        default:
            break;
        }
    }else{
        if(_cur)
        {
            switch(event->key())
            {
            case Qt::Key_F1:
                QMessageBox::information(this, "快捷键功能", helpinfo);
                break;
            case Qt::Key_Plus:
                gaoliang(_cur->fei(1));
                break;
            case Qt::Key_Minus:
                gaoliang(_cur->fei(-1));
                break;
            case Qt::Key_X:
                _cur->rmmask(QMGONG9::MASK_JIXING1 | QMGONG9::MASK_JIXING2);
                break;
            case Qt::Key_K:
                _cur->rmmask(QMGONG9::MASK_KONGWANG1 | QMGONG9::MASK_KONGWANG2);
                break;
            case Qt::Key_P:
                _cur->rmmask(QMGONG9::MASK_MENPO);
                break;
            case Qt::Key_M:
                _cur->rmmask(QMGONG9::MASK_RUMU1 | QMGONG9::MASK_RUMU2);
                break;
            default:
                break;
            }
            _cur->shengxiangyi();
            _cur->jiexi();
            _displays[_cur->_xu]->setText( _cur->_xiangyi);
        }

        switch(event->key())
        {
        case Qt::Key_D:
            fullshow();
            break;
        case Qt::Key_N :
            gaoliang(_gong9.teshu( EQM_YS_NGAN));
            break;
        case Qt::Key_Y :
            gaoliang(_gong9.teshu( EQM_YS_YGAN));
            break;
        case Qt::Key_R :
            gaoliang(_gong9.teshu( EQM_YS_RGAN));
            break;
        case Qt::Key_S :
            gaoliang(_gong9.teshu( EQM_YS_SGAN));
            break;
        case Qt::Key_Z:
            gaoliang(_gong9.teshu(EQM_YS_ZHFU));
            break;
        case Qt::Key_1 ... Qt::Key_9 :
            gaoliang(_gong9.gong(event->key() - Qt::Key_0));
            break;
        default:
            break;
        }
    }

}

void QMDJWIN::closeEvent(QCloseEvent *)
{
}


void QMDJWIN::on_textBrowser0_selectionChanged()
{
    gaoliang(_gong9.xu(0));
}


void QMDJWIN::on_textBrowser1_selectionChanged()
{
    gaoliang(_gong9.xu(1));
}


void QMDJWIN::on_textBrowser2_selectionChanged()
{
    gaoliang(_gong9.xu(2));
}

void QMDJWIN::on_textBrowser3_selectionChanged()
{
    gaoliang(_gong9.xu(3));
}


void QMDJWIN::on_textBrowser4_selectionChanged()
{
    gaoliang(_gong9.xu(4));
}


void QMDJWIN::on_textBrowser5_selectionChanged()
{
    gaoliang(_gong9.xu(5));
}


void QMDJWIN::on_textBrowser6_selectionChanged()
{
    gaoliang(_gong9.xu(6));
}


void QMDJWIN::on_textBrowser7_selectionChanged()
{
    gaoliang(_gong9.xu(7));
}


void QMDJWIN::on_textBrowser8_selectionChanged()
{
    gaoliang(_gong9.xu(8));
}

void QMDJWIN::on_ysgTableWidget_cellClicked(int row, int column)
{
    int g;
    column = 4;
    g = ui->ysgTableWidget->item(row, column)->text().toInt();

    if(g > 0)
        gaoliang(_gong9.gong(g));
}


void QMDJWIN::initcbx()
{
    ui->tiaoComboBox->addItem("--", EQM_YS_TESHU);
    ui->tiaoComboBox->addItem("九星", EQM_YS_XING9);
    ui->tiaoComboBox->addItem("八门", EQM_YS_MMEN8);
    ui->tiaoComboBox->addItem("八神", EQM_YS_SHEN8);
    ui->tiaoComboBox->addItem("天盘", EQM_YS_TPGAN);
    ui->tiaoComboBox->addItem("地盘", EQM_YS_DPGAN);
}




